<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" size="small" :inline="true" label-width="68px">
      <el-form-item prop="storageId">
        <el-select v-model="queryParams.storageId" placeholder="请选择仓库" clearable>
          <el-option v-for="item in storages" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="categoryId">
        <el-cascader :options="categories" :props="{ checkStrictly: true ,value:'id'}" placeholder="请选择类目" clearable filterable
          @change="onSelectCategory"/>
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="queryParams.name" clearable placeholder="请输入商品名称"/>
      </el-form-item>
      <el-form-item prop="type" style="width:100px">
        <el-select v-model="queryParams.type" clearable placeholder="请选择销售状态">
          <el-option v-for="dict in dict.type.warning_num"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item prop="minNum" style="width:110px">
        <el-input v-model="queryParams.minNum" clearable placeholder="请输入最小值"/>
      </el-form-item>
      <el-form-item style="color:#d2d3d7">
        —
      </el-form-item>
      <el-form-item prop="maxNum" style="width:110px">
        <el-input v-model="queryParams.maxNum" clearable placeholder="请输入最大值"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item prop="showType">
        <el-checkbox v-model="queryParams.showType" :true-label="1" :false-label="0" @change="handleQuery">只显示库存预警</el-checkbox>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="storageGoodsList">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="{$index}">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + $index + 1 }}
        </template>
      </el-table-column>
      <!--<el-table-column v-if="true" label="主键" align="center" prop="id" />-->
      <el-table-column label="仓库名称" align="left" header-align="center" prop="storageName" />
      <el-table-column label="商品类目" align="left" header-align="center" prop="categoryName" />
      <!--<el-table-column label="商品条码" align="left" header-align="center" prop="barCode" />-->
      <!--<el-table-column label="商品ID" align="left" header-align="center" prop="productId" />-->
      <el-table-column label="商品名称" align="left" header-align="center" prop="productName" />
      <el-table-column label="商品规格" align="left" header-align="center" prop="productAttrName" />
      <el-table-column label="现货量" align="center" header-align="center">
        <template slot-scope="{row}">
          {{ (Number(row.frezzStock)+Number(row.stock)).toFixed(0) }}
        </template>
      </el-table-column>
      <el-table-column label="可售量" align="center" header-align="center" prop="stock">
        <template slot-scope="{row}">
          <div :class="{'stock--warning': row.stock < row.warningNum}">{{ row.stock }}</div>
        </template>
      </el-table-column>
      <el-table-column label="锁定量" align="center" header-align="center" prop="frezzStock" />
      <el-table-column label="预警量" align="center" header-align="center" prop="warningNum">
        <template slot-scope="{row}">
          <el-button type="text" @click="updateWarningNum(row)">{{ row.warningNum }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <el-dialog :visible.sync="open" title="预警量设置" width="25%">
      <el-form ref="form" :model="form" :rules="rules" inline>
        <el-form-item prop="warningNum" label="预警值">
          <el-input v-model="form.warningNum" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { warningList, warningUpdate } from '@/api/storage/goods'
import { listAllStorage } from '@/api/storage/storage'
import { listStoreCategoryTree } from '@/api/product/storeCategory'
// import NP from 'number-precision'

export default {
  name: 'StorageGoods',
  dicts: ['warning_num'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // // 非单个禁用
      // single: true,
      // // 非多个禁用
      // multiple: true,
      // // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库商品表格数据
      storageGoodsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storageId: undefined,
        categoryId: undefined,
        type: undefined,
        name: undefined,
        minNum: undefined,
        maxNum: undefined,
        showType: 0
      },
      // 表单参数
      form: {
        warningNum: 0
      },
      // 表单校验
      rules: {
        warningNum: { required: true, message: '请输入预警量', trigger: 'blur' }
      },
      storages: [],
      categories: []
      // editMode: {}
    }
  },
  created() {
    this.getList()
    this.listAllStorage()
    this.listStoreCategoryTree()
  },
  methods: {
    listAllStorage() {
      listAllStorage().then(({ data }) => {
        this.storages = data
      })
    },
    listStoreCategoryTree() {
      listStoreCategoryTree().then(({ data }) => {
        this.categories = data.content
      })
    },
    /** 查询仓库商品列表 */
    getList() {
      this.loading = true
      // this.editMode = {}
      warningList(this.queryParams).then(response => {
        this.storageGoodsList = response.rows
        this.total = response.total
        // response.rows.forEach(({ id }) => {
        //   this.$set(this.editMode, id, false)
        // })
      }).finally(() => {
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    onSelectCategory(value) {
      this.queryParams.categoryId = value[value.length - 1]
    },
    warningUpdate(num) {
      const { storageId, productId, productAttrId } = this.$_currentRow
      return warningUpdate({ storageId, productId, productAttrId, num })
    },
    updateWarningNum(row) {
      this.$_currentRow = row
      this.form.warningNum = row.warningNum
      this.open = true
    },
    onSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.warningUpdate(this.form.warningNum).then(() => {
            this.open = false
            this.getList()
          })
        }
      })
    }
  }
}
</script>
<style>

.stock--warning {
  background: #f00;
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center
}
</style>
